// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/face/api/FaceLibApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/face/model/FaceLibReq'
import DelClass from '@api/face/model/FaceLibDeleteReq'
import PassClass from '@api/face/model/FaceLibControlStatusReq'
import BaseClass from '@api/face/model/FaceLibDto'
import ChildClass from '@api/face/model/FaceDeviceDto'

const OneClass = ListClass
const AddClass = ListClass
const EditClass = ListClass
const RejClass = PassClass

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const CHILDREN = [ChildClass, 'faceDeviceDto', 1]
const BASE = [BaseClass, 'dto', '', (dto, obj) => CORE.grpcDTO(dto, obj, CHILDREN)]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询人脸库列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM, [BaseClass, 'dto'])
    const resp = await CORE.grpc(TIP, CLIENT, 'faceLibQuery', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },
  transList(arr) {
    arr.forEach((item) => {
      const successDevice = item.faceDeviceDto.filter((item) => item.syncStatus === 3)
      item.syncCount = `( ${successDevice.length} / ${item.faceDeviceDto.length} )`
    })
    return arr
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne(one, TIP = '查询人脸库详情') {
    const DTO = CORE.grpcDTO(OneClass, { id: one.id }, [BaseClass, 'dto'])
    const resp = await CORE.grpc(TIP, CLIENT, 'faceLibQuery', DTO)
    return (resp.content || [])[0] || one
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增人脸库') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'faceLibSave', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改人脸库') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'faceLibUpdate', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除人脸库') {
    const DTO = CORE.grpcDTO(DelClass, { ids: id })
    await CORE.grpc(TIP, CLIENT, 'faceLibDelete', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 通过 —————————————————————————————— //

  async pass({ id }, TIP = '布控') {
    const DTO = CORE.grpcDTO(PassClass, { faceLibId: id, controlStatus: 1 })
    await CORE.grpc(TIP, CLIENT, 'faceLibControlStatus', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 拒绝 —————————————————————————————— //

  async rej({ id }, TIP = '撤控') {
    const DTO = CORE.grpcDTO(RejClass, { faceLibId: id, controlStatus: 2 })
    await CORE.grpc(TIP, CLIENT, 'faceLibControlStatus', DTO)
    return CORE.grpcOK(TIP)
  },
}
